use crate::public::Rule;
use sonic_rs::JsonValueTrait;
use std::collections::HashSet;

pub struct Rest;

impl Rule for Rest {
    async fn get_rule(&self) -> HashSet<String> {
        let client = reqwest::Client::new();
        let resp = client
            .get("https://api.btse.com/futures/api/v2.3/market_summary")
            .send()
            .await
            .unwrap();

        let body = resp.text().await.unwrap();

        let array = sonic_rs::to_array_iter(body.as_str());

        let mut set = HashSet::new();

        for symbol in array {
            let obj = symbol.unwrap();

            if obj.get("active").as_bool().unwrap() != true
                || obj.get("quote").as_str().unwrap() != "USDT"
                || obj.get("timeBasedContract").as_bool().unwrap() != false
            {
                continue;
            }

            let symbol = format!(
                "{}",
                obj.get("symbol").as_str().unwrap().replace("-PERP", "USDT")
            );

            set.insert(symbol);
        }

        set
    }
}
